﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.DBUtility;
using Hangjing.WebCommon;

/// <summary>
/// 弹窗提交订单
/// </summary>
public partial class qy_54tss_Admin_Service_submitorderbox : System.Web.UI.Page
{
    Points daltogo = new Points();
    Hangjing.SQLServerDAL.ETogoShoppingCart dal = new Hangjing.SQLServerDAL.ETogoShoppingCart();

    protected IList<Hangjing.Model.ETogoShoppingCart> Foods
    {
        set
        {
            ViewState["Foods"] = value;
        }
        get
        {
            return ViewState["Foods"] == null ? null : (IList<Hangjing.Model.ETogoShoppingCart>)ViewState["Foods"];
        }
    }

    /// <summary>
    /// 经度
    /// </summary>
    protected string blng
    {
        set
        {
            ViewState["blng"] = value;
        }
        get
        {
            return ViewState["blng"] == null ? "" : ViewState["blng"].ToString();
        }
    }

    /// <summary>
    /// 纬度
    /// </summary>
    protected string blat
    {
        set
        {
            ViewState["blat"] = value;
        }
        get
        {
            return ViewState["blat"] == null ? "" : ViewState["blat"].ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int id = HjNetHelper.GetQueryInt("id", 0);


            string usercode = WebUtility.FixgetCookie("uc");

            Foods = dal.GetCart(usercode);
            //计算配送费

            //计算配送费
            blat = WebUtility.FixgetCookie("mylat");
            blng = WebUtility.FixgetCookie("mylng");

            hidLat.Value = blat;
            hidLng.Value = blng;

            string strDistance = " 1=1 ";
            IList<PointsInfo> shoplist = daltogo.GetDistanceListSuper(1, 1, "Points.unid=" + id, "unid", 1, blat, blng, strDistance);
            PointsInfo model = shoplist[0];
            hftid.Value = id.ToString();
            hidTogoName.Value = model.Name;
         
            decimal m_totalmoney = 0;
            int m_allnum = 0;
            foreach (Hangjing.Model.ETogoShoppingCart item in Foods)
            {
                for (int i = 0; i < item.ItemList.Count; i++)
                {
                    m_totalmoney += item.ItemList[i].PPrice * item.ItemList[i].PNum;
                    m_allnum += item.ItemList[i].PNum;
                }
            }
            tbPrice.Value = m_totalmoney.ToString();

            PointsInfo shop = daltogo.GetModel(id);
            tbCommission.Value = WebHelper.getShopCommission(m_totalmoney, shop).ToString();

         

            string myaddrid = WebUtility.FixgetCookie("used_addressid");
            if (myaddrid != null && myaddrid != "")
            {
                int myid = Convert.ToInt32(myaddrid);
                EAddressInfo addrmodel = new EAddress().GetModel(myid);
                if (addrmodel != null)
                {
                    tbuname.Text = addrmodel.Receiver;
                    tbaddress.Text = addrmodel.Address;

                }
              
                tbtel.Text = HjNetHelper.GetQueryString("tel");

            }

            OrderTime ot = new OrderTime(ddltime, model);

        }
    }

    protected void add_click(object sender, EventArgs e)
    {
        int id = HjNetHelper.GetQueryInt("id", 0);
        PointsInfo shop = daltogo.GetModel(id);

        string usercode = WebUtility.FixgetCookie("uc");

        Custorder orderdal = new Custorder();
        CustorderInfo infos = new CustorderInfo();
        infos.OrderRcver = WebUtility.InputText(this.tbuname.Text);
        string maddress = WebUtility.InputText(this.tbaddress.Text);
        infos.AddressText = maddress;
        infos.OrderComm = WebUtility.InputText(this.tbtel.Text.Trim());
        infos.UserId = 0;
        infos.OrderAttach = WebUtility.InputText(this.tbremark.Value);

        infos.cityid = 0;
        infos.OrderAddress = UserHelp.GetAdmin().AdminName;
        infos.ReveInt1 = 1;
        infos.ReveInt2 = 0;
        infos.fromweb = ((int)OrderSource.CallCenter).ToString();
        infos.tempcode = usercode;
        infos.SendTime = Convert.ToDateTime(ddltime.SelectedValue);
        infos.tempcode = "";
        infos.CustomerName = "";
        infos.ReveVar1 = shop.sentorg;

        string ulat = hidLat.Value;
        string ulng = hidLng.Value;

        infos.OrderSums = Convert.ToDecimal(tbPrice.Value);
        infos.shopdiscountmoney = Convert.ToDecimal(tbshopdiscountmoney.Value);
        infos.OldPrice = infos.OrderSums;
        infos.cityid = shop.cityid;
        infos.TogoId = shop.Unid;
        infos.P2Sign = "";
        infos.paytime = Convert.ToDateTime("1970-1-1");
        infos.paymode = 4;
        infos.ReveDate1 = DateTime.Now;
        infos.cardpay = Convert.ToDecimal(tbcardpay.Value);
        infos.cardids = WebUtility.InputText(tbcardids.Value);

        infos.Commission = Convert.ToDecimal(tbCommission.Value);
        infos.settlecycle = shop.PType;
        infos.Advance = WebUtility.InputText(this.tbAdvance.Value,'c');

        infos.Mergeorderremark = WebUtility.InputText(tbMergeorderremark.Value);

        infos.delivergetfee_base = WebUtility.InputText(this.tbdelivergetfee_base.Value, 'c');


        infos.SystemUserId = Convert.ToInt32(ddlSystemUserId.SelectedValue);
        infos.IsShopSet = 1;
        infos.ReveDate1 = DateTime.Now;

        IList<Hangjing.Model.ETogoShoppingCart> list = Foods;

        foreach (Hangjing.Model.ETogoShoppingCart item in Foods)
        {
            item.latlng = "{'ulat':'" + blat + "','ulng':'" + blng + "','slat':'" + item.Lat + "','slng':'" + item.Lng + "'}";
            infos.ReveVar2 = item.latlng;
        }

        try
        {
            IList<Hangjing.Model.ROrderinfo> mylist = orderdal.SubmitOrder(Foods, infos);
            if (mylist != null)
            {
                dal.DelAllCartItem(usercode);
                WebUtility.FixdelCookie("crm_tell");
                WebUtility.FixdelCookie("crm_uid");
                WebUtility.FixdelCookie("used_addressid");

                if (infos.IsShopSet == 0)
                {
                    foreach (var item in mylist)
                    {
                        NoticeHelper notice = new NoticeHelper(Context, item.togoid.ToString());
                        notice.send2ShopByShopid();
                    }
                }

               

                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单提交成功!');top.location.href  = top.location.href;");
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单失败!')");
            }
        }
        catch (Exception ex)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('订单失败,请联系管理员!')");
            Hangjing.AppLog.AppLog.Error("提交订单失败:管理员=" + UserHelp.GetAdmin() + "\r\n订单信息=" + Newtonsoft.Json.JsonConvert.SerializeObject(infos) + "\r\n错误信息=" + ex.ToString());

        }

       
    }

}
